package com.icim.xmtm005.exporter.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.icim.csot.exporter.tx.csotshipping.CsotShippingO;
import com.icim.dao.BisDataDao;
import com.icim.dao.HisRetPrdInfoDao;
import com.icim.dao.RetBoxDao;
import com.icim.dao.RetPrdInfoDao;
import com.icim.dao.RetPrdJgeDao;
import com.icim.dao.RetTmCusG5PrdInDao;
import com.icim.dao.RetTmCusG5PrdIndtDao;
import com.icim.dao.RetWoDao;
import com.icim.dao.SpcRetMfgDataDao;
import com.icim.entity.Bis_data;
import com.icim.entity.His_ret_prd_info;
import com.icim.entity.Ret_box;
import com.icim.entity.Ret_prd_info;
import com.icim.entity.Ret_prd_jge;
import com.icim.entity.Ret_tm_cus_g5_prd_in;
import com.icim.entity.Ret_tm_cus_g5_prd_indt;
import com.icim.entity.Ret_wo;
import com.icim.entity.Spc_ret_mfg_data;
import com.icim.exception.ICIMException;
import com.icim.util.TimeUtil;
import com.icim.xmtm.dao.CtSlimifRetRacktHistoryDao;
import com.icim.xmtm.entity.CtSlimifRetRacktHistory;
import com.icim.xmtm005.dao.CtSlimifRetBox005Dao;
import com.icim.xmtm005.dao.CtSlimifRetDefectData005Dao;
import com.icim.xmtm005.dao.CtSlimifRetProduct005Dao;
import com.icim.xmtm005.entity.CtSlimifRetBox005;
import com.icim.xmtm005.entity.CtSlimifRetDefectData005;
import com.icim.xmtm005.entity.CtSlimifRetProduct005;

@Service
public class XmtmExportShippingData005Service implements IXmtmExportShippingData005Service {

	private static List<String> mesIdList = new ArrayList<String>();

	@Resource
	private RetBoxDao retBoxDao;

	@Resource
	private RetPrdInfoDao retPrdInfoDao;

	@Resource
	private RetPrdJgeDao retPrdJgeDao;

	@Resource
	private CtSlimifRetBox005Dao ctSlimifRetBox005Dao;
	
	@Resource
	private CtSlimifRetRacktHistoryDao ctSlimifRetRacktHistoryDao;

	@Resource
	private CtSlimifRetProduct005Dao ctSlimifRetProduct005Dao;

	@Resource
	private CtSlimifRetDefectData005Dao ctSlimifRetDefectData005Dao;

	@Resource
	private BisDataDao bisDataDao;

	@Resource
	private RetWoDao retWoDao;

	@Resource
	private RetTmCusG5PrdInDao retTmCusG5PrdInDao;

	@Resource
	private RetTmCusG5PrdIndtDao retTmCusG5PrdIndtDao;

	@Resource
	private SpcRetMfgDataDao spcRetMfgDataDao;

	@Resource
	private HisRetPrdInfoDao hisRetPrdInfoDao;

	@Resource
	DataSourceTransactionManager xmtm005TransactionManager;

	static {
		mesIdList.add("CDPC");
		mesIdList.add("CZPC");
	}

	@Override
	public CsotShippingO sendShippingData(String palletId,String shipAddress) throws Exception {
		CsotShippingO outTrx = new CsotShippingO();
		List<Ret_box> boxList = retBoxDao.getBoxList(palletId);
		Timestamp currentTimestamp = TimeUtil.getCurrentTimestamp();
		
		Map<String,String> shipAddressMap = new HashMap<String, String>();
        if (StringUtils.isNotBlank(shipAddress)) {
          ObjectMapper mapper = new ObjectMapper();
          shipAddressMap = mapper.readValue(shipAddress, Map.class);
        }
        System.out.println("005成品出货回传资料:"+shipAddress.toString());

		Map<String, Bis_data> defectMap = getDefectMap(boxList);

		String woId = null;
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus transaction = xmtm005TransactionManager.getTransaction(def);
		try {
			for (Ret_box retBox : boxList) {
				String boxId = retBox.getBox_id();

				CtSlimifRetBox005 ctSlimifRetBox = new CtSlimifRetBox005();
				ctSlimifRetBox.setBoxrealid(retBox.getBoxrealid());
				ctSlimifRetBox.setVendorcode("WG");// TODO

				ctSlimifRetBox.setProductnumber(retBox.getLot_id_fk());
				ctSlimifRetBox.setBoxname(boxId);
				ctSlimifRetBox.setBoxquantity(retBox.getPrd_qty());

				ctSlimifRetBox.setReceiveflag("N");
				ctSlimifRetBox.setReleaseflag("N");
				ctSlimifRetBox.setUnpackflag("N");
				ctSlimifRetBox.setDeliverytime(null);
				ctSlimifRetBox.setReceivestate("N");
				
				String shipAddres = shipAddressMap.get(retBox.getBox_id());
                System.out.println("EDI----005交货地址："+shipAddres+";箱号："+retBox.getBox_id());
                String [] shipAddressArr = StringUtils.isBlank(shipAddres) ? null : shipAddres.split("%");
                String address = shipAddres == null || shipAddres.length() < 2 ? "" : shipAddressArr[0]; 
                ctSlimifRetBox.setDestination(address);

				woId = retBox.getWo_id_fk();
				List<Ret_wo> retWoList = retWoDao.getWo(woId);
                Ret_wo retWo = null;
                if (!retWoList.isEmpty()) {
                    retWo = retWoList.get(0);
                }
                
                if (retWo.getWo_cate().equals("RMA")) {
                  if(StringUtils.isNotBlank(retBox.getExt_9())){
                    ctSlimifRetBox.setBoxname(retBox.getExt_9());
                    boxId = retBox.getExt_9();
                  }
                }

				setBoxThicknessProc(retWo, ctSlimifRetBox,retBox);

				Ret_tm_cus_g5_prd_in retTmCusPrdIn = retTmCusG5PrdInDao.get(boxId);
				if (retTmCusPrdIn != null) {
					ctSlimifRetBox.setProducttype(retTmCusPrdIn.getProducttype());
					ctSlimifRetBox.setBoxtype(retTmCusPrdIn.getBoxtype());
					ctSlimifRetBox.setProductspecname(retTmCusPrdIn.getProductspecname());
					ctSlimifRetBox.setTargetproductspecname(retTmCusPrdIn.getTargetproductspecname());
				}else {
                  List<Ret_prd_info> prdList = retPrdInfoDao.getSheetList(retBox.getBox_id());
                  if (!prdList.isEmpty()) {
                    String sheetId = prdList.get(0).getPrd_seq_id();
                    Ret_tm_cus_g5_prd_indt indt = retTmCusG5PrdIndtDao.get(sheetId);
                    if (indt != null) {
                      Ret_tm_cus_g5_prd_in prdIn = retTmCusG5PrdInDao.get(indt.getBoxname());
                      if (prdIn != null) {
                        ctSlimifRetBox.setProducttype(prdIn.getProducttype());
                        ctSlimifRetBox.setBoxtype(prdIn.getBoxtype());
                        ctSlimifRetBox.setProductspecname(prdIn.getProductspecname());
                        ctSlimifRetBox.setTargetproductspecname(prdIn.getTargetproductspecname());
                      }
                    }
                  }
                }

				List<Ret_tm_cus_g5_prd_indt> retTmCusPrdIndtList = retTmCusG5PrdIndtDao.list(boxId);
				Map<Integer, String> sourceSlotSheetMap = new HashMap<Integer, String>();
				for (Ret_tm_cus_g5_prd_indt retTmCusPrdIndt : retTmCusPrdIndtList) {
					sourceSlotSheetMap.put(retTmCusPrdIndt.getPosition(), retTmCusPrdIndt.getProductname());
				}

				List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.getSheetList(retBox.getBox_id());

				Set<String> sheetIdSet = new HashSet<String>();
				for (Ret_prd_info rePrdInfo : retPrdInfoList) {
					sheetIdSet.add(rePrdInfo.getPrd_seq_id());
				}

				Map<String, List<Ret_prd_jge>> defectSheetMap = retPrdJgeDao.getDefectMapBySheets(sheetIdSet);

				Map<String, His_ret_prd_info> sheetHisMap = hisRetPrdInfoDao
						.findSheetsStbiHistory(sheetIdSet.toArray(new String[sheetIdSet.size()]));

				List<CtSlimifRetProduct005> ctSlimifRetProductList = new ArrayList<CtSlimifRetProduct005>();

				List<Integer> slotList = new ArrayList<Integer>();

				List<CtSlimifRetDefectData005> ctSlimifRetDefectDataList = new ArrayList<CtSlimifRetDefectData005>();

				StringBuffer sheetIdsSql = new StringBuffer(""); 
				for (int i = 0; i < retPrdInfoList.size(); i++) {
				    sheetIdsSql.append("'").append(retPrdInfoList.get(i).getPrd_seq_id()).append("'");
				    if (i<retPrdInfoList.size()-1) {
                      sheetIdsSql.append(",");
                    }
					CtSlimifRetProduct005 ctSlimifRetProduct = new CtSlimifRetProduct005();
					Ret_prd_info sheet = retPrdInfoList.get(i);
					
					slotList.add(Integer.valueOf(sheet.getSlot_no()));
					int xCnt = sheet.getX_axis_cnt();
					int yCnt = sheet.getY_axis_cnt();
					int integration = xCnt * yCnt;
					ctSlimifRetBox.setIntegrationlevel(String.valueOf(integration));

					ctSlimifRetProduct.setVendorcode("WG");// TODO
					String sheetId = sheet.getPrd_seq_id();
					if (StringUtils.contains(sheetId, "_")) {
                      sheetId = sheetId.split("_")[0];
                    }
					ctSlimifRetProduct.setProductname(sheetId);

					ctSlimifRetProduct.setBoxname(sheet.getBox_id_fk());
                    if (retWo.getWo_cate().equals("RMA")) {
                      if(StringUtils.isNotBlank(retBox.getExt_9())){
                        ctSlimifRetProduct.setBoxname(retBox.getExt_9());
                      }
                    }

					Integer slotNo = Integer.valueOf(sheet.getSlot_no());
					String sourceSheetId = sourceSlotSheetMap.get(slotNo);
					if (!StringUtils.isEmpty(sourceSheetId)) {
						ctSlimifRetProduct.setShipproduct(sourceSheetId);
					}
					ctSlimifRetProduct.setProductgrade(sheet.getGrade());
					ctSlimifRetProduct.setTrackintime(sheet.getAct_stb_timestamp());
					ctSlimifRetProduct.setTrackouttime(currentTimestamp);
					ctSlimifRetProduct.setPosition(Integer.valueOf(sheet.getSlot_no()));
					ctSlimifRetProduct.setDescription(sheet.getDs_recipe_id());
					ctSlimifRetProduct.setBoxrealid(retBox.getBoxrealid());
					ctSlimifRetProductList.add(ctSlimifRetProduct);

					List<Ret_prd_jge> defectList = defectSheetMap.get(sheetId);

					if (defectList != null && !defectList.isEmpty()) {

						List<CtSlimifRetDefectData005> iqcDefectList = setIQCProc(defectMap, sheetHisMap, sheetId,
								defectList);

						if (!iqcDefectList.isEmpty()) {
							ctSlimifRetDefectDataList.addAll(iqcDefectList);
						}
						List<CtSlimifRetDefectData005> oqcDefectList = setOQCProc(defectMap, defectList);
						if (!oqcDefectList.isEmpty()) {
							ctSlimifRetDefectDataList.addAll(oqcDefectList);
						}
					}
					
					List<His_ret_prd_info> hisList = hisRetPrdInfoDao.findSheetsHistoryForRet005(sheet.getPrd_seq_id());
	                if (!hisList.isEmpty()) {
	                  for (His_ret_prd_info info : hisList) {
	                    CtSlimifRetRacktHistory retHistory = new CtSlimifRetRacktHistory();
	                    retHistory.setFactoryname("WG");
	                    retHistory.setCusInfoSnd(info.getCus_info_fst());
	                    retHistory.setMtrlProdIdFk(info.getFm_mdl_id_fk());
	                    retHistory.setMtrlBoxIdFk(info.getMtrl_box_id_fk());
	                    retHistory.setPrdSeqId(info.getPrd_seq_id());
	                    retHistory.setCrToolIdFk(info.getCr_tool_id_fk());
	                    retHistory.setEvtCate(info.getEvt_cate());
	                    retHistory.setEvtTimestamp(info.getEvt_timestamp());
	                    if ("LGOF".equals(info.getEvt_cate())) {
	                      retHistory.setCfPolishTimeSpend(info.getC_rubbing_time());
	                      retHistory.setTftPolishTimeSpend(info.getT_rubbing_time());
	                      List<Spc_ret_mfg_data> spcDatas = spcRetMfgDataDao.getCsotSpcDataRecordersForRet005(info.getPrd_seq_id());
	                      if (!spcDatas.isEmpty()) {
	                        retHistory.setEvtUsr(spcDatas.get(0).getData_value_001());
	                        retHistory.setPolishPressure2(spcDatas.get(0).getData_value_002());
	                        retHistory.setUpDiskFrequency(spcDatas.get(0).getData_value_003());
	                        retHistory.setDownDiskFrequency(spcDatas.get(0).getData_value_004());
	                        retHistory.setSwingRange(spcDatas.get(0).getData_value_005());
	                        retHistory.setAxleDistance(spcDatas.get(0).getData_value_006());
	                      }
	                    }
	                    ctSlimifRetRacktHistoryDao.save(retHistory);
	                  }
	                }

				}

				ctSlimifRetDefectDataList.addAll(addCdpcCzPcProc(sheetIdSet));

				StringBuffer slotMap = new StringBuffer();
				for (int i = 0; i < retBox.getStd_qty(); i++) {
					slotMap.append(slotList.contains(i + 1) ? "O" : "X");
				}
				ctSlimifRetBox.setSlotmap(slotMap.toString());

				if (!ctSlimifRetDefectDataList.isEmpty()) {
					ctSlimifRetDefectData005Dao.saveBatch(ctSlimifRetDefectDataList);
				}

				ctSlimifRetProduct005Dao.saveBatch(ctSlimifRetProductList);

				ctSlimifRetBox005Dao.save(ctSlimifRetBox);

				

			}
			xmtm005TransactionManager.commit(transaction);
		} catch (Exception ex) {
			xmtm005TransactionManager.rollback(transaction);
			throw ex;
		}

		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("SUCCESS");
		return outTrx;
	}

	public List<CtSlimifRetDefectData005> setIQCProc(Map<String, Bis_data> defectMap,
			Map<String, His_ret_prd_info> sheetHisMap, String sheetId, List<Ret_prd_jge> defectList) {
		String opeId = getStbiOpe(sheetHisMap, sheetId);

		List<CtSlimifRetDefectData005> iqcDefectList = new ArrayList<CtSlimifRetDefectData005>();
		for (Ret_prd_jge defect : defectList) {

			if (!StringUtils.isEmpty(opeId) && defect.getOpe_id_fk().equals(opeId)) {

				CtSlimifRetDefectData005 ctSlimifRetDefectData = new CtSlimifRetDefectData005();
				ctSlimifRetDefectData.setVendorcode("WG");// TODO
				ctSlimifRetDefectData.setProductname(defect.getPrd_seq_id_fk());
				ctSlimifRetDefectData.setDefecttype("DEFECT");
				ctSlimifRetDefectData.setDefectxyaxis(defect.getDef_code1_lc());
				ctSlimifRetDefectData.setDefectside(defect.getDef_code1_sd());
				ctSlimifRetDefectData.setInspectoperation("IQC");

				Bis_data bisData = defectMap.get(defect.getDef_code1());
				if (bisData != null) {
					ctSlimifRetDefectData.setDefectdescription(bisData.getData_ext());
					ctSlimifRetDefectData.setDefectcode(bisData.getExt_5());
				}
				// ctSlimifRetDefectDataDao.save(ctSlimifRetDefectData);
				iqcDefectList.add(ctSlimifRetDefectData);
			}
		}
		return iqcDefectList;
	}

	private String getStbiOpe(Map<String, His_ret_prd_info> sheetHisMap, String sheetId) {
		String opeId = null;
		His_ret_prd_info stbiSheetHistory = sheetHisMap.get(sheetId);

		if (stbiSheetHistory != null) {
			opeId = stbiSheetHistory.getCr_ope_id_fk();
		}
		return opeId;
	}

	private List<CtSlimifRetDefectData005> setOQCProc(Map<String, Bis_data> defectMap, List<Ret_prd_jge> defectList) {
		List<CtSlimifRetDefectData005> ctSlimifRetDefectDataList = getOQCDefectData(defectMap, defectList);

		return ctSlimifRetDefectDataList;
		// if (!ctSlimifRetDefectDataList.isEmpty()) {
		// ctSlimifRetDefectDataDao.saveBatch(ctSlimifRetDefectDataList);
		// }
	}

	private List<CtSlimifRetDefectData005> getOQCDefectData(Map<String, Bis_data> defectMap,
			List<Ret_prd_jge> defectList) {
		List<CtSlimifRetDefectData005> ctSlimifRetDefectDataList = new ArrayList<CtSlimifRetDefectData005>();

		for (Ret_prd_jge defect : defectList) {

			if (defect.getDef_code1_cnt() == -1) {
				continue;
			} else {
				boolean existFlg = false;
				for (Ret_prd_jge defect2 : defectList) {
					if (defect2.getDef_code1_lc().equals(defect.getDef_code1_lc())
							&& defect2.getDef_code1_sd().equals(defect.getDef_code1_sd())
							&& defect2.getDef_code1().equals(defect.getDef_code1())
							&& defect2.getDef_code1_cnt() == -1) {
						existFlg = true;
						break;
					}
				}

				if (existFlg) {
					continue;
				}
			}

			CtSlimifRetDefectData005 ctSlimifRetDefectData = new CtSlimifRetDefectData005();
			ctSlimifRetDefectData.setVendorcode("WG");// TODO
			ctSlimifRetDefectData.setProductname(defect.getPrd_seq_id_fk());
			ctSlimifRetDefectData.setDefecttype("DEFECT");
			ctSlimifRetDefectData.setDefectxyaxis(defect.getDef_code1_lc());
			ctSlimifRetDefectData.setDefectside(defect.getDef_code1_sd());
			ctSlimifRetDefectData.setInspectoperation("OQC");

			Bis_data bisData = defectMap.get(defect.getDef_code1());
			if (bisData != null) {
				ctSlimifRetDefectData.setDefectdescription(bisData.getData_ext());
				ctSlimifRetDefectData.setDefectcode(bisData.getExt_5());
			}

			ctSlimifRetDefectDataList.add(ctSlimifRetDefectData);
		}
		return ctSlimifRetDefectDataList;
	}

	private void setBoxThicknessProc(Ret_wo retWo, CtSlimifRetBox005 ctSlimifRetBox, Ret_box retBox) {
  	    if (retWo == null) {
          return;
        }

		Double fromThickness = retWo.getFrom_thickness();
		Double toThickness = retWo.getTo_thickness();
		ctSlimifRetBox.setThickness(fromThickness == null ? "" : fromThickness.toString());
		ctSlimifRetBox.setTargetthickness(toThickness == null ? "" : toThickness.toString());
	}

	public List<CtSlimifRetDefectData005> addCdpcCzPcProc(Set<String> sheetIdSet) {
		List<Spc_ret_mfg_data> spcRetMfgDataList = spcRetMfgDataDao.getCsotSpcDataRecorderByShtIdsMesIds(sheetIdSet,
				mesIdList);
		Map<String, Spc_ret_mfg_data> cdpcEdcMap = new HashMap<String, Spc_ret_mfg_data>();
		Map<String, Spc_ret_mfg_data> czpcEdcMap = new HashMap<String, Spc_ret_mfg_data>();

		getEdcMap(spcRetMfgDataList, cdpcEdcMap, czpcEdcMap);

		List<CtSlimifRetDefectData005> ctSlimifRetDefectDataList = new ArrayList<CtSlimifRetDefectData005>();

		setCtDefectEdcEntityList(cdpcEdcMap, ctSlimifRetDefectDataList, "CDPC");
		setCtDefectEdcEntityList(czpcEdcMap, ctSlimifRetDefectDataList, "CZPC");

		return ctSlimifRetDefectDataList;
	}

	private void setCtDefectEdcEntityList(Map<String, Spc_ret_mfg_data> cdpcEdcMap,
			List<CtSlimifRetDefectData005> ctSlimifRetDefectDataList, String mesId) {

		Set<Entry<String, Spc_ret_mfg_data>> cdpcEntry = cdpcEdcMap.entrySet();
		for (Entry<String, Spc_ret_mfg_data> cdpc : cdpcEntry) {
			Spc_ret_mfg_data spcRetMfgData = cdpc.getValue();
			if (spcRetMfgData == null) {
				continue;
			}
			Integer dataCnt = spcRetMfgData.getData_cnt();
			for (int i = 0; i < dataCnt; i++) {
				CtSlimifRetDefectData005 ctSlimifRetDefectData = new CtSlimifRetDefectData005();
				ctSlimifRetDefectData.setVendorcode("WG");// TODO
				ctSlimifRetDefectData.setProductname(spcRetMfgData.getPrd_seq_id_fk());
				ctSlimifRetDefectData.setDefecttype(mesId);
				ctSlimifRetDefectData.setDefectposition(i + 1 + "");
				try {
					ctSlimifRetDefectData.setDefectcode(
							spcRetMfgData.getPropertyValue(Spc_ret_mfg_data.SPC_RET_MFG_DATA_VALUE_COLUMN_LIST.get(i)));
				} catch (Exception ex) {
					throw new ICIMException(ex);
				}
				ctSlimifRetDefectDataList.add(ctSlimifRetDefectData);
			}

		}
	}

	private void getEdcMap(List<Spc_ret_mfg_data> spcRetMfgDataList, Map<String, Spc_ret_mfg_data> cdpcEdcMap,
			Map<String, Spc_ret_mfg_data> czpcEdcMap) {
		for (Spc_ret_mfg_data newEdc : spcRetMfgDataList) {
			String newSheetId = newEdc.getPrd_seq_id_fk();
			if (newEdc.getMes_id_fk().equals("CDPC")      // && newEdc.getData_pat_fk().equals("T")
					&& !newEdc.getOpe_id_fk().equals("JB-01")&& !newEdc.getOpe_id_fk().equals("JB-02")
					&& !newEdc.getOpe_id_fk().equals("JB-03")&& !newEdc.getOpe_id_fk().equals("JB-04")
					&& !newEdc.getOpe_id_fk().equals("JB-05")&& !newEdc.getOpe_id_fk().equals("JB-06")
					&& !newEdc.getOpe_id_fk().equals("JB-07")) { // TODO
				Spc_ret_mfg_data oldEdc = cdpcEdcMap.get(newSheetId);
				if (oldEdc == null) {
					cdpcEdcMap.put(newSheetId, newEdc);
				} else {
					if (newEdc.getRpt_timestamp().after(oldEdc.getRpt_timestamp())) {
						cdpcEdcMap.put(newSheetId, newEdc);
					}
				}
			} else if (newEdc.getMes_id_fk().equals("CZPC") 
					&& !newEdc.getOpe_id_fk().equals("JB-06")&& !newEdc.getOpe_id_fk().equals("JB-07")) {
				Spc_ret_mfg_data oldEdc = czpcEdcMap.get(newSheetId);
				if (oldEdc == null) {
					czpcEdcMap.put(newSheetId, newEdc);
				} else {
					if (newEdc.getRpt_timestamp().after(oldEdc.getRpt_timestamp())) {
						czpcEdcMap.put(newSheetId, newEdc);
					}
				}
			}

		}
	}

	private Map<String, Bis_data> getDefectMap(List<Ret_box> boxList) {
		Map<String, Bis_data> defectMap = new HashMap<String, Bis_data>();
		if (!boxList.isEmpty()) {
			List<Bis_data> bisDataList = bisDataList = bisDataDao.listData("DFCT", null, null, null, null, null, null,
					null, null);
			for (Bis_data bisData : bisDataList) {
				defectMap.put(bisData.getData_id(), bisData);
			}
		}
		return defectMap;
	}
}
